<template>
  <div class="addStudent">
    <el-dialog :title="examineeObj.ifOrder === '1' ? '添加学生-根据导入的学生数据进行分组': '添加学生-普通导入学生列表'" :lock-scroll="false" :close-on-click-modal="false" :fullscreen="true"
      :visible.sync="addVisable" width="860px" :before-close="handleClose">
      <div class="content">
        <div class="top">
          <p>基本信息</p>
          <div class="base-info">
            <div>学生库名称：<span style="font-weight: 600;">{{ examineeObj.studentPool }}</span></div>
            <div style="padding-left: 40px;">创建时间：<span style="font-weight: 600;">{{ examineeObj.creatDate }}</span></div>
          </div>
        </div>
        <div class="top mid">
          <p>学生列表 
            <el-button type="text" style="margin-left: 10px;">
              <a
              download="学生列表模板.xlsx"
              href="https://www.solacemeta.com/osceadmin/osce-template/student.xlsx"
              class="btn"
              style="color:#2d8cf0;"
              >下载模板</a>
            </el-button>
          </p>
          <div class="mid-bot-import">
           <studentList ref="studentList"></studentList>
          </div>
        </div>
        <div class="bot">
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="getStudentP">保存</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import studentList from './studentList.vue'
import {importObj} from '@/api/examinee.js'
export default{
  components: {
    studentList
  },
  props: {
    addVisable: {
      type: Boolean,
      default: false
    },
    examineeObj: {
      type: Object,
      default: {}
    }
  },
  data () {
    return {
      studentData: []
    }
  },
  methods: {
    close () {
      this.$emit('child-event')
    },
    handleClose () {
      this.$emit('child-event')
    },
    resetList () {
      setTimeout(() => {
        this.$refs.studentList.resetDataChild()
      }, 100)
    },
    getStudentP () {
      let aa = this.$refs.studentList.getStudent()
      if (aa.length === 0) {
        this.$message.warning('请先导入学生列表')
      } else {
        this.studentData = []
        aa.map(item => {
          this.studentData.push({
            studentName: item.head0,
            studentIdcard: item.head1,
            studentCandidateId: item.head2,
            studentMobile: item.head3,
            trainingBase: item.head4,
            // 专业
            deptName: item.head5,
            // 考点名称
            examLocation: item.head6,
            // 考核时间
            examTime: item.head7,
            // 报到时间
            checkTime: item.head8,
            // 场次地址
            examAddress: item.head9,
            round: item.head10,
            team: item.head11,
            stationName: item.head12,
            longitude: item.longitude,
            latitude: item.latitude,
            examLongitude: item.examLongitude,
            examLatitude: item.examLatitude,
            creatUserId: this.$store.state.user.id,
            creatUserName: this.$store.state.user.realName
          })
        })
        let timeArrBak = this.studentData.map(v => {
          return v.examTime
        })
        let teamCountArr = []
        let stationCountArr = []
        let timeArr = JSON.stringify([...new Set(timeArrBak)])
        if (this.studentData[0].team && this.studentData[0].team.length > 0) {
          let teamCountArrBak = this.studentData.map(v => {
            return parseInt(v.team.substring(1))
          })
          teamCountArr = [...new Set(teamCountArrBak)].sort((a, b) => parseInt(a) - parseInt(b))
        }
        if (this.studentData[0].stationName && this.studentData[0].stationName.length > 0) {
          let stationCountArrBak = this.studentData.map(v => {
            return parseInt(v.stationName.substring(1))
          })
          stationCountArr = [...new Set(stationCountArrBak)].sort((a, b) => parseInt(a) - parseInt(b))
        }

        console.log('stationCountArr', stationCountArr)
        console.log('teamCountArr', teamCountArr)
        let teamCount = ''
        let stationCount = ''
        if (teamCountArr.length > 0) {
          teamCount = teamCountArr[teamCountArr.length - 1]
        }
        if (stationCountArr.length > 0) {
          stationCount = stationCountArr[stationCountArr.length - 1]
        }
        console.log(teamCount, stationCount)
        let params = {
          studentPoolId: this.examineeObj.id,
          students: this.studentData,
          time: timeArr,
          teamCount: teamCount,
          stationCount: stationCount,
          // 考点名称
          examLocation: aa[0].head6,
          // 考点地址
          examAddress: aa[0].head9,
          longitude: aa[0].examLongitude,
          latitude: aa[0].examLatitude
        }
        importObj(params).then(data => {
          if (data.data && data.data.code === 0) {
            this.$message.success('导入成功')
            this.handleClose()
          }
        })
      }
    }
  }
}
</script>
<style lang="scss">
.addStudent{
  .el-dialog__body{
    background: #f5f5f5;
  }
}
.content{
  height: 100%;
  background: #fff;
  overflow: auto;
  padding: 10px;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  color: #333;
  .top{
    p{
      font-weight: 600;
      font-size: 16px;
      text-indent: 10px;
      position: relative;
      padding: 0;
      margin: 0;
    }
    p:before{
      content: '';
      height: 100%;
      width: 4px;
      border-radius: 4px;
      position: absolute;
      left: 0;
      top: 0;
      background: #00b99e;
      font-size: 14px;
    }
    .base-info{
      padding: 10px 0 10px 10px;
      display: flex;
      justify-content: flex-start;
    }
  }
  .mid{
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: auto;
    p:before{
      content: '';
      height: 20px;
      width: 4px;
      border-radius: 4px;
      position: absolute;
      left: 0;
      top: 8px;
      background: #00b99e;
    }
    .mid-bot-import-import{
      flex: 1;
      overflow: auto;
      padding: 10px;
    }
  }
  .bot{
    height: 40px;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}
</style>